



package wviz.gui.graph;

/**
 * A pair of terms (words) with socres as well as a score for label between these two classes.
 * Example of this class:
 *    -----                ------
 *    | W1  |     SL       | W2  |
 *    |     |--------------|     |
 *    | S1  |              | S2  |
 *    -----                ------
 *  SL = Score of the link
 * <p/>
 *
 * @author Mohammad Asgari
 * @version 1.0
 * @date 13-Jan-2009
 */
public class ScoredTermPair<S> {
    private final ScoredTerm term1;
    private final ScoredTerm term2;
    private final S linkScore;

    public ScoredTermPair(ScoredTerm term1, ScoredTerm term2) {
        this.term1 = term1;
        this.term2 = term2;
        this.linkScore = null;
    }

    public ScoredTermPair(ScoredTerm term1, ScoredTerm term2, S linkScore) {
        this.term1 = term1;
        this.term2 = term2;
        this.linkScore = linkScore;
    }

    public ScoredTerm getTerm1() {
        return term1;
    }

    public ScoredTerm getTerm2() {
        return term2;
    }

    public S getLinkScore() {
        return linkScore;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ScoredTermPair that = (ScoredTermPair) o;

        //first check terms to each other, consider that it is possible to exchange t1 with t2
        if (term1.equals(that.term1) && !term2.equals(that.term2)) return false;
        if (term1.equals(that.term2) && !term2.equals(that.term1)) return false;//this line can be OR with prev. line but don't do that!

        //second check link score
        if ((linkScore != null) && (!linkScore.equals(that.linkScore))) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = term1.hashCode() + term2.hashCode();
        if (linkScore != null)
            result = 31 * result + linkScore.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return String.format("[%s]-%s-[%s]", term1, linkScore, term2);
    }

}
